import React from "react";
// 注意从dva/router里面引入
import { Router, Route, Switch } from "dva/router";
import Layout from "../layout/layout";
// import HomePage from "../pages/hp"
import Login from "../pages/login"
import locale from "antd/lib/locale/zh_CN";
import { ConfigProvider } from "antd";

const listenRouter = (history) => {
  const global = JSON.parse(localStorage.getItem("global") || "{}");
  history.listen((params) => {
    if (
      !global.token &&
      params.pathname !== "/login" &&
      history.location.pathname !== "/login"
    ) {
      history.push("/login");
    }
  });
};

function RouterConfig({ history }) {
  listenRouter(history);

  return (
    <ConfigProvider locale={locale}>
      <Router history={history}>
        <Switch>
          <Route path="/login">
            <Login></Login>
          </Route>
          <Route path="/" >
            <Layout></Layout>
          </Route>
        </Switch>
      </Router>
    </ConfigProvider>
  );
}

export default RouterConfig;
